<?php
include ('../models/Admin.php');
include ('../models/Course.php');
include ('../models/Student.php');

$admin = new Admin();
$course = new Course();
$student = new Student();

if (isset($_GET['action'])) {
    $getAction = $_GET['action'];
    switch ($getAction) {
        case 'showAllCrouse':
            $res=$course->showAllCrouse();
            print_r($res);
        break;
        case 'showAllStudentByCourse':
            $courseName=$_GET['courseName'];
            $res=$course->showAllStudentByCourse($courseName);
            print_r($res);
        break;
        case 'showAllInfoByStudent':
            $studentId=$_GET['studentId'];
            $res=$course->showAllInfoByStudent($studentId);
            print_r($res);
        break;
        case 'loginOut':
            $student->loginOut();
            header('Location: /studentSkill/views/login.php');
        break;
        case 'adLoginOut':
            $admin->adLoginOut();
            header('Location: /studentSkill/views/adLogin.php');
        break;
        case 'showAllStudent':
            $res=$student->showAll();
            print_r($res);
        break;        
        case 'studentDel':
            $id = $_GET['id'];
            if ($student->del($id)) {
                header('Location: /studentSkill/views/adStudents.php');
            } else {
                header('Location: /studentSkill/views/actionError.php');
            }
        break;
        case 'adCourseDel':
            $id = $_GET['id'];
            if ($course->del($id)) {
                header('Location: /studentSkill/views/adCourses.php');
            } else {
                header('Location: /studentSkill/views/actionError.php');
            }
        break;    
        default:
            echo "err get";
        break;
    }
} elseif (isset($_POST['action'])) {
    $postAction = $_POST['action'];
    switch ($postAction) {
        case 'adLogin':
            $username = $_POST['username'];
            $password = $_POST['password'];
            if ($admin->adLogin($username, $password)) {
                header("Location: /studentSkill/views/adIndex.php?page=adIndex");
            } else {
                header("Location: /studentSkill/views/adLogin.php?page=adLogin");
            }
        break;
        case 'adCourseInsert':
            $studentId = $_POST['studentId'];
            $courseName = $_POST['courseName'];
            $courseScore = $_POST['courseScore'];
            $weight = $_POST['weight'];
            $kind=$_POST['kind'];
            echo $kind;
            if ($course->insert($studentId, $courseName, $courseScore, $weight,$kind)) {
                header('Location: /studentSkill/views/adCourses.php');
            } else {
                header('Location: /studentSkill/views/actionError.php');
            }
        break;
        case 'adCourseUpdate':
            $id = $_POST['id'];
            $weight = $_POST['weight'];
            $courseScore = $_POST['courseScore'];
            if ($course->update($id,$courseScore,$weight)) {
                header('Location: /studentSkill/views/adCourses.php');
            } else {
                header('Location: /studentSkill/views/actionError.php');
            }
        break;
        case 'login':
            $studentId = $_POST['studentId'];
            $studentPwd = $_POST['studentPwd'];
            if ($student->login($studentId, $studentPwd)) {
                header("Location: /studentSkill/views/index.php");
            } else {
                header("Location: /studentSkill/views/login.php");
            }
        break;
        case 'studentInsert':
            $studentName = $_POST['studentName'];
            $studentId = $_POST['studentId'];
            $studentPwd = $_POST['studentPwd'];
            if ($student->insert($studentId,$studentName,$studentPwd)) {
                header('Location: /studentSkill/views/adStudents.php');
            } else {
                header('Location: /studentSkill/views/actionError.php');
            }
        break;

        default:
            echo "err post";
        break;
    }
} else {
    return false;
}
?>